package com.netease.streamlearningbyjava;

import com.netease.streamlearningbyjava.bean.Employee;

import org.junit.Test;

import java.sql.ClientInfoStatus;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

/**
 * 创建日期：2021/11/3 10:12
 *
 * @author tony.sun
 * 类说明：
 */

public class Stream9mapping {
    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 58, 5555.55),
            new Employee("王五", 26, 3333.33),
            new Employee("赵六", 36, 6666.66),
            new Employee("田七", 12, 8888.88),
            new Employee("田七", 12, 8888.88)
    );

    @Test
    public void test1() {
        List<String> list = Arrays.asList("aaa", "bbb", "ccc", "ddd", "eee");
        //list里面的每个转为大写
        list
                .stream()//转流
                .map((str) -> str.toUpperCase())//调用map方法，对每个值进行操作，这里是对每个值进行大写化
                .forEach(System.out::println);//对每个值进行遍历，
        /**
         * 获取list里面的 bean里的一个值
         */
        System.out.println("----------------------------------------------");
        employees.stream()
                .map(Employee::getName)
                .forEach(System.out::println);
        System.out.println("----------------------------------------------");
        Stream<Stream<Character>> stream = list.stream()
                //filterCharacter方法在这里不需要传入参数，因为map这个家伙已经拿出来每个值，然后对每个值进行操作，这里它将其给filterCharacter，相当于传入了值
                .map(Stream9mapping::filterCharacter);//{a,a,a},{b,b,b}，……
        stream.forEach((sm) -> {
            sm.forEach(System.out::println);//{a,a,a,b,b,b,……}
        });
        Stream<Character> characterStream = list.stream()
                .flatMap(Stream9mapping::filterCharacter);//{a,a,a,b,b,b,……}
        characterStream.forEach(System.out::println);
    }

    /**
     * 对字符串里的每个字符拿出来，添加到list里面，然后返回list
     *
     * @param str string原料
     * @return 返回字符list
     */
    public static Stream<Character> filterCharacter(String str) {
        List<Character> list = new ArrayList<>();//创建一个字符list
        //对字符串进行遍历，并添加到一个字符list
        for (char ch : str.toCharArray()) {
            list.add(ch);
        }
        //返回这个字符流
        return list.stream();
    }

    @Test
    public void test6() {
        List<String> list = Arrays.asList("aaa", "bbb", "ccc", "ddd");
        List list2 = new ArrayList();
        list2.add(11);
        list2.add(22);
//        list2.add(list);//[11, 22, [aaa, bbb, ccc, ddd]]
        list2.addAll(list);//[11, 22, aaa, bbb, ccc, ddd]
        System.out.println(list2);
    }

    /**
     * 排序
     * sorted（）自然排序(Comparable):abcde来排
     * sorted(comparator com)--定制排序(comparator)
     */
    @Test
    public void test7() {
        List<String> list = Arrays.asList("aaa", "ccc", "bbb", "ddd", "eee");
        list.stream()
                .sorted()
                .forEach(System.out::println);
        System.out.println("-------------------------------------");
        employees.stream()
                .sorted(Comparator.comparing(Employee::getAge).thenComparing(Employee::getName,Comparator.reverseOrder()))
                .forEach(System.out::println);

    }
}
